#include"CollisionManager.h"
#include<math.h>

CubeList::~CubeList()
{
	CubeNode *nodePTR, *nextNode;
	nodePTR = head;
	while(nodePTR != NULL)
	{
		nextNode = nodePTR->next;
		delete nodePTR;
		nodePTR = nextNode;
	}
}

void CubeList::appendNode(cube &object)
{
	CubeNode *newNode, *nodePTR;

	newNode = new CubeNode;
	newNode->value = &object;
	newNode->next = NULL;
	newNode->previous = NULL;

	if(!head)
	{
		head = newNode;
		tail = newNode;
	}
	else
	{
		nodePTR = tail;
		/*while(nodePTR->next)
		{
			nodePTR = nodePTR->next;
		}*/
		nodePTR->next = newNode;
		newNode->previous = nodePTR;
		tail = newNode;
	}
}


GroundList::~GroundList()
{
	GroundNode *nodePTR, *nextNode;
	nodePTR = head;
	while(nodePTR != NULL)
	{
		nextNode = nodePTR->next;
		delete nodePTR;
		nodePTR = nextNode;
	}
}

void GroundList::appendNode(ground &object)
{
	GroundNode *newNode, *nodePTR;

	newNode = new GroundNode;
	newNode->value = &object;
	newNode->next = NULL;
	newNode->previous = NULL;

	if(!head)
	{
		head = newNode;
		tail = newNode;
	}
	else
	{
		nodePTR = tail;
		/*while(nodePTR->next)
		{
			nodePTR = nodePTR->next;
		}*/
		nodePTR->next = newNode;
		newNode->previous = nodePTR;
		tail = newNode;
	}
}

bool CollisionManager::testGroundCollision(int num)
{
	CubeNode *object;
	GroundNode *platform;

	object = Enemies.head;

	if(num != object->value->ID)
	{
		object = object->next;
		while(object->value->ID != num)
		{
			object = object->next;
		}
	}

	platform = Platforms.head;

	if(Platforms.head)
	{
		while(platform != NULL)
		{
			if(object->value->transX >= (platform->value->transX - 5) && object->value->transX <= (platform->value->transX + 5))
			{
				if((object->value->transY - 1.41) >= (platform->value->transY - 2.82) && (object->value->transY - 1.41) <= (platform->value->transY - 1.41))
				{
					return true;
				}
			}
			platform = platform->next;
		}
	}

	return false;
}

bool CollisionManager::testObjectCollision(int num)
{
	CubeNode *object;
	CubeNode *enemy;

	float col_vi_Y = 0;
	float col_vi_X = 0;

	float tempx, tempy;

	float* highx, highy;
	float* lowx, lowy;

	object = Enemies.head;

	if(num != object->value->ID)
	{
		object = object->next;
		while(object->value->ID != num)
		{
			object = object->next;
		}
	}

	enemy = Enemies.head;

	if(Enemies.head)
	{
		while(enemy != NULL)
		{
			if(enemy->value->ID != object->value->ID)
			{
				/*double test;  
				test = pow((object->value->transX - enemy->value->transX), 2);
				double test2;
				test2 = pow((object->value->transY - enemy->value->transY), 2);
				double test3;
				test3 = test + test2;*/
				if(
					pow((object->value->transX - enemy->value->transX), 2) 
					+ pow((object->value->transY - enemy->value->transY), 2) 
					<= pow(2.5, 2)
				  )
				{
					// this is oncomplete, would require more physics, prob not gonna do it.
					if(object->value->vi_X >= 0 && enemy->value->vi_X <= 0)
					{
						tempx = object->value->vi_X;
						object->value->vi_X = enemy->value->vi_X;
						enemy->value->vi_X = tempx;
					}
					else if(object->value->vi_X <= 0 && enemy->value->vi_X >= 0)
					{
						tempx = object->value->vi_X;
						object->value->vi_X = enemy->value->vi_X;
						enemy->value->vi_X = tempx;
					}
					else
					{
						if(abs(object->value->vi_X) > abs(enemy->value->vi_X))
						{
							highx = &object->value->vi_X;
							lowx = &enemy->value->vi_X;
						}
						else
						{
							lowx = &object->value->vi_X;
							highx = &enemy->value->vi_X;
						}
					}
					
					tempy = object->value->vi_Y;
					object->value->vi_Y = enemy->value->vi_Y;
					enemy->value->vi_Y = tempy;
					return true;
				}
			}
			enemy = enemy->next;
		}
	}

	return false;
}